package com.jltx.xq.simplewatch.utils;

import com.jltx.xq.simplewatch.base.App;

import java.io.File;
import java.io.IOException;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author jltxseo
 * @date 2017/09/05.
 * @description:
 */

public class OkhttpClientFactoryUtils {
    private static final String CACHE_CONTROL = "Cache-Control";
    private static int cacheSize = 10 * 1024 * 1024; // 10 MiB
    private  OkHttpClient okHttpClient;

    public OkhttpClientFactoryUtils() {
        File httpCacheDirectory = new File(App.getApp().getApplicationContext().getCacheDir(), "swCache");
        Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Response originalResponse = chain.proceed(chain.request());
                if (NetWorkUtil.isNetWorkAvailable(App.getApp().getApplicationContext())) {
                    int maxAge = 60; // 在线缓存在1分钟内可读取
                    return originalResponse.newBuilder()
                            .removeHeader("Pragma")
                            .removeHeader(CACHE_CONTROL)
                            .header(CACHE_CONTROL, "public, max-age=" + maxAge)
                            .build();
                } else {
                    int maxStale = 60 * 60 * 24 * 28; // 离线时缓存保存4周
                    return originalResponse.newBuilder()
                            .removeHeader("Pragma")
                            .removeHeader(CACHE_CONTROL)
                            .header(CACHE_CONTROL, "public, only-if-cached, max-stale=" + maxStale)
                            .build();
                }
            }
        };
        Cache cache = new Cache(httpCacheDirectory, cacheSize);

        /**
         * 增加头部信息的拦截器,获取解析今日视频需要用到
         */
        Interceptor mHeaderInterceptor = chain -> {
            Request.Builder builder = chain.request().newBuilder();
            builder.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.0.0.13547");
            builder.addHeader("Cache-Control", "max-age=0");
            builder.addHeader("Upgrade-Insecure-Requests", "1");
            builder.addHeader("X-Requested-With", "XMLHttpRequest");
            builder.addHeader("Cookie", "uuid=\"w:f2e0e469165542f8a3960f67cb354026\"; __tasessionId=4p6q77g6q1479458262778; csrftoken=7de2dd812d513441f85cf8272f015ce5; tt_webid=36385357187");
            return chain.proceed(builder.build());
        };

        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(mHeaderInterceptor)
                .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
                .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
                .cache(cache)
                .build();
    }

    public OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }

    public static OkhttpClientFactoryUtils getInstance(){
        return OkhttpClientFactoryHolder.sInstance;
    }


    private static class OkhttpClientFactoryHolder{
        private static final OkhttpClientFactoryUtils sInstance = new OkhttpClientFactoryUtils();
    }
}
